package com.kgc.web;

import com.kgc.dao.PaymentDao;
import com.kgc.entity.Payment;
import com.kgc.untils.PageUtils;
import com.kgc.vo.R;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "PaymentServlet", value = "/PaymentServlet")
public class PaymentServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        PaymentDao paymentDao = new PaymentDao();
        String opt = request.getParameter("opt");
        if ("list".equals(opt)) {    //数据显示和模糊查询
            String name = request.getParameter("name");
            System.out.println("name = " + name);
            // 获取page和limit
            String page = request.getParameter("page");
            String limit = request.getParameter("limit");
            PageUtils pageUtils = new PageUtils();
            int total = paymentDao.total(name);
            if (StringUtils.isNotEmpty(page)) {
                pageUtils.setCurrPageNo(Integer.parseInt(page));
            }
            if (StringUtils.isNotEmpty(limit)) {
                pageUtils.setPageSize(Integer.parseInt(limit));
            }
            pageUtils.init(total);
            //查寻数据
            List<Payment> list = paymentDao.selectAll(pageUtils, name);
            response.getWriter().write(R.success("用户列表加载成功...", total, list).toJSON());
        } else if ("add".equals(opt)) {   //新增数据
            String name = request.getParameter("name");
            int count = paymentDao.addpayment(new Payment(name));
            if (count > 0) {
                response.getWriter().write(R.success("缴费内容添加成功!").toJSON());
            } else {
                response.getWriter().write(R.error("缴费内容添加失败!").toJSON());
            }
        } else if ("delete".equals(opt)) {   //删除数据
            String id = request.getParameter("id");
            int count = paymentDao.deletePayment(id);
            if (count > 0) {
                response.getWriter().write(R.success("删除成功!").toJSON());
            } else {
                response.getWriter().write(R.error("删除失败,数据正在使用中!").toJSON());
            }
        } else if ("edit".equals(opt)) { //修改数据
            String id = request.getParameter("id");
            String name = request.getParameter("name");
            int count = paymentDao.editPayment(new Payment(Integer.parseInt(id), name));
            if (count > 0) {
                response.getWriter().write(R.success("修改成功!").toJSON());
            } else {
                response.getWriter().write(R.error("修改失败,数据正在使用中!").toJSON());
            }
        } else if ("Payment".equals(opt)) {
            List<Payment> paymentList = paymentDao.selectPayment();
            response.getWriter().write(R.success("费用列表加载成功...",paymentList).toJSON());
        }

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
